package com.aisb.common;

import com.aisb.core.Result;
import com.aisb.core.SessionBean;
import com.aisb.core.util.file.ExportExcelUtils;
import com.aisb.manage.AuthService;
import com.aisb.manage.UserService;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/common")
public class CommonController {
    private static Logger logger = LoggerFactory.getLogger(CommonController.class);

    @Autowired
    CommonService commonService;

    @Autowired
    AuthService authService;

    @Autowired
    UserService userService;

    @RequestMapping(value = "/navs")
    @ResponseBody
    public Result navList(@RequestParam Map param) {
        SessionBean sessionBean = (SessionBean) SecurityUtils.getSubject().getPrincipal();
        param.put("userId", sessionBean.getUserId());
        param.put("isAdmin", sessionBean.getAdmin());

        List rlist = commonService.navList(param);

        return new Result(true, rlist);
    }

    @RequestMapping(value = "/areas")
    @ResponseBody
    public Result areaList(@RequestParam Map param) {
        List rlist = commonService.areaSimpleAll();

        return new Result(true, rlist);
    }

    @RequestMapping(value = "/roles")
    @ResponseBody
    public Result roleList(@RequestParam Map param) {
        List rlist = authService.roleList();

        return new Result(true, rlist);
    }

    @RequestMapping(value = "/theme/save")
    @ResponseBody
    public Result themeSave(@RequestParam Map param) {
        SessionBean sessionBean = (SessionBean) SecurityUtils.getSubject().getPrincipal();

        sessionBean.setUserTheme((String) param.get("theme"));

        param.put("userId", sessionBean.getUserId());
        int result = userService.userModify(param, false);

        return new Result(true, result);
    }

    @RequestMapping(value = "/download")
    public void export(HttpServletResponse response, @RequestParam("file") String file) {
        try {
            ExportExcelUtils.exportExcel(response, file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
